package com.itheima.controller;

import com.itheima.Utils.JwtUtils;
import com.itheima.pojo.*;
import com.itheima.services.UserServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UserController {
    @Autowired
    public UserServices userServices;

    @PostMapping("/Ulogin")
    public Result Ulogin(@RequestBody User user){
        System.out.println("Ulogin");
        User user1=userServices.Ulogin(user);
        if(user1!=null){
            Map<String,Object> claims=new HashMap<>();
            claims.put("name",user1.getName());
            claims.put("password",user1.getPassword());
            String jwt= JwtUtils.generateJwt(claims);
            System.out.println(jwt);
            return Result.success(jwt);
        }
        return Result.error();
    }

    @PostMapping("/registered")
    public Result registered(@RequestBody User user){
        System.out.println("registered");
        Result result=userServices.registered(user);
        return result;
    }

    @GetMapping("/getImage")
    public Result getImage(String userName){
        System.out.println("getImage");
        String image=userServices.getImage(userName);
        return Result.success(image);
    }
    @GetMapping("/getUserMessage")
    public Result getUserMessage(String userName){
        System.out.println("getUserMessage");
        UserMessage userMessage=userServices.getUserMessage(userName);
        return Result.success(userMessage);
    }
    @PostMapping("/changeUserMessage")
    public Result changeUserMessage(@RequestBody UserMessage userMessage){
        System.out.println("changeUserMessage");
        System.out.println(userMessage);
        Result result=userServices.changeUserMessage(userMessage);
        return result;
    }
    @PostMapping("/uShowFood")
    public Result uShowFood(@RequestBody Food food,String userName){
        System.out.println("ushowFood");
        List<Shopping> foods=userServices.uShowFood(food,userName);
        return Result.success(foods);
    }
    @GetMapping("/minusFood")
    public Result minusFood(String userName,String foodName){
        System.out.println("minusFood");
        userServices.minusFood(userName,foodName);
        return Result.success();
    }
    @GetMapping("/plusFood")
    public Result plusFood(String userName,String foodName){
        System.out.println("plusFood");
        userServices.plusFood(userName,foodName);
        return Result.success();
    }
    @GetMapping("/getList")
    public Result getList(String userName){
        System.out.println("getList");
        String list=userServices.getList(userName);
        System.out.println(list);
        return Result.success(list);
    }
    @GetMapping("/submitOrder")
    public Result submitOrder(String userName,String list,String remark){
        System.out.println("submitOrder");
        Result result=userServices.submitOrder(userName,list,remark);
        return result;
    }
    @GetMapping("/uShowOrders")
    public Result uShowOrders(String userName,int currentPage,int pageSize){
        System.out.println("ushowOrders");
        System.out.println(userName);
        PageBean2 pageBean=userServices.uShowOrders(userName,currentPage,pageSize);
        System.out.println(pageBean);
        return Result.success(pageBean);
    }

    @GetMapping("/giveRate")
    public Result giveRate(Long orderId,int rate){
        System.out.println("giveRate");
        System.out.println(orderId);
        System.out.println(rate);
        userServices.giveRate(orderId,rate);
        return Result.success();
    }

    @GetMapping("/ifCancel")
    public Result ifCancel(String userName){
        System.out.println("ifCancel");
        Result result=userServices.ifCancel(userName);
        return result;
    }
}
